﻿using BillHelper;
using DemoForBillOcr;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaxAssistantBase;

namespace ShiBieProject
{
    class InvoiceNumCheck
    {
        public static InvoiiceOcrModel getInvoiceInfo(InvoiiceOcrModel bpm)
        {
            InvoiiceOcrModel iomode = new InvoiiceOcrModel();
            string tpresult = "";
            string apiurl = ApiUrlList.GetInvoiceData;
            InvoiiceOcrModel invoiceInfo = new InvoiiceOcrModel();
            bpm.invoiceNum = bpm.invoiceNum;
            try {
                string result = RestDataTool.PostInvoiceInfo(out tpresult, iomode, apiurl, out invoiceInfo);
                if (invoiceInfo != null)
                {
                    if (invoiceInfo.invoiceNum==null) {
                        return bpm;
                    }
                    bpm.billingDate = invoiceInfo.billingDate;
                    bpm.invoiceNum = invoiceInfo.invoiceNum;
                    bpm.companyState = CompanyOrMoneyStateEnum.MoneyCalculationAbsolutelyRight;
                    if (bpm.invoiceOcrSonLists != null)
                    {
                        if (bpm.invoiceOcrSonLists.Count() > 0)
                        {
                            bpm.invoiceOcrSonLists[0].invoiceNum = invoiceInfo.invoiceOcrSonLists[0].invoiceNum;
                            bpm.invoiceOcrSonLists[0].invoiceDate = invoiceInfo.invoiceOcrSonLists[0].invoiceDate;
                            bpm.invoiceOcrSonLists[0].invoiceDateInitialValue = invoiceInfo.invoiceOcrSonLists[0].invoiceDate;
                            bpm.invoiceOcrSonLists[0].moneyAmounts = invoiceInfo.invoiceOcrSonLists[0].moneyAmounts;
                            bpm.invoiceOcrSonLists[0].moneyAmountsInitialValue = invoiceInfo.invoiceOcrSonLists[0].moneyAmounts;
                            bpm.invoiceOcrSonLists[0].taxAmounts = invoiceInfo.invoiceOcrSonLists[0].taxAmounts;
                            bpm.invoiceOcrSonLists[0].taxAmountsInitialValue = invoiceInfo.invoiceOcrSonLists[0].taxAmounts;
                            bpm.invoiceOcrSonLists[0].taxRate = invoiceInfo.invoiceOcrSonLists[0].taxRate;
                            bpm.invoiceOcrSonLists[0].taxRateInitialValue = invoiceInfo.invoiceOcrSonLists[0].taxRate;
                            bpm.invoiceOcrSonLists[0].totalMoney = invoiceInfo.invoiceOcrSonLists[0].totalMoney;
                            bpm.invoiceOcrSonLists[0].totalMoneyInitialValue = invoiceInfo.invoiceOcrSonLists[0].totalMoney;
                            bpm.invoiceOcrSonLists[0].invoicetype = invoiceInfo.invoiceOcrSonLists[0].invoicetype;
                            bpm.invoiceOcrSonLists[0].invoicetypeInitialValue = invoiceInfo.invoiceOcrSonLists[0].invoicetype;
                            if (invoiceInfo.invoiceOcrSonLists[0].gfcompanyname != null)
                            {
                                bpm.invoiceOcrSonLists[0].gfcompanyname = invoiceInfo.invoiceOcrSonLists[0].gfcompanyname;
                                bpm.invoiceOcrSonLists[0].gfcompanynameInitialValue = invoiceInfo.invoiceOcrSonLists[0].gfcompanyname;
                                bpm.invoiceOcrSonLists[0].gftaxnumber = invoiceInfo.invoiceOcrSonLists[0].gftaxnumber;
                                bpm.invoiceOcrSonLists[0].gftaxnumberInitialValue = invoiceInfo.invoiceOcrSonLists[0].gftaxnumber;
                                bpm.invoiceOcrSonLists[0].isProviderOrCustomer = "0";//销项票为主公司
                            }
                            if (invoiceInfo.invoiceOcrSonLists[0].xfcompanyname != null)
                            {
                                bpm.invoiceOcrSonLists[0].xfcompanyname = invoiceInfo.invoiceOcrSonLists[0].xfcompanyname;
                                bpm.invoiceOcrSonLists[0].xfcompanynameInitialValue = invoiceInfo.invoiceOcrSonLists[0].xfcompanyname;
                                bpm.invoiceOcrSonLists[0].xftaxnumber = invoiceInfo.invoiceOcrSonLists[0].xftaxnumber;
                                bpm.invoiceOcrSonLists[0].xftaxnumberInitialValue = invoiceInfo.invoiceOcrSonLists[0].xftaxnumber;
                                bpm.invoiceOcrSonLists[0].isProviderOrCustomer = "1";//进项票为主公司
                            }

                            bpm.invoiceOcrSonLists[0].invoicetype = invoiceInfo.invoiceOcrSonLists[0].invoicetype;
                            bpm.invoiceOcrSonLists[0].invoicetypeInitialValue = invoiceInfo.invoiceOcrSonLists[0].invoicetype;
                            bpm.invoiceOcrSonLists[0].totalStateMoney = CompanyOrMoneyStateEnum.MoneyCalculationAbsolutelyRight;
                            if (invoiceInfo.invoiceOcrDetailedDtoList!=null) {
                                if (invoiceInfo.invoiceOcrDetailedDtoList.Count()>0) {
                                    bpm.invoiceOcrDetailedDtoList = invoiceInfo.invoiceOcrDetailedDtoList;
                                    foreach (invoiceOcrDetailedModel idm in bpm.invoiceOcrDetailedDtoList ) {
                                        idm.moneyAmountInitialValue = idm.moneyAmount;
                                        idm.taxAmountInitialValue = idm.taxAmount;
                                        idm.detaileTaxRateInitialValue = idm.detaileTaxRateInitialValue;
                                        idm.productNameInitialValue = idm.productName;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex) {
                WsdLogger.yxlog("error", "完整批次处理报错" + ex.Message + "公司名称：" + bpm.companyName);
                return bpm;
            }
            return bpm;
        }
    }
}
